package cn.harmonyoslearn.navrouter.datas;


import cn.harmonyoslearn.navrouter.utils.JsonUtil;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.Objects;

public class RouterMapInfo {
    public String name;
    public String buildFunction;
    public String pageSourceFile;

    public RouterMapInfo(String name, String buildFunction, String pageSourceFile) {
        this.name = name;
        this.buildFunction = buildFunction;
        this.pageSourceFile = pageSourceFile;
    }

    public RouterMapInfo(JsonObject object) {
        name = JsonUtil.jsonObject2String(Objects.requireNonNull(object.get("name")));
        buildFunction = JsonUtil.jsonObject2String(Objects.requireNonNull(object.get("buildFunction")));
        pageSourceFile = JsonUtil.jsonObject2String(Objects.requireNonNull(object.get("pageSourceFile")));
    }

    public String toString() {
        return RouterMapInfo.toJsonString(this);
    }

    public static String toJsonString(RouterMapInfo routerMapInfo) {
        return "{" + "\"name\":\"" + routerMapInfo.name + "\",\"buildFunction\":\"" + routerMapInfo.buildFunction
                + "\",\"pageSourceFile\":\"" + routerMapInfo.pageSourceFile+ "\"}";
    }

    public static String toJsonString(ArrayList<RouterMapInfo> routerMapInfos) {
        StringBuilder str = new StringBuilder("[");

        for (RouterMapInfo routerMapInfo : routerMapInfos) {
            str.append(toJsonString(routerMapInfo)).append(",");
        }

        str.delete(str.length()-1, str.length());
        return str + "]";
    }
}
